package com.kamistoat.meimeimall.meimeimallsearch.web;

import com.kamistoat.meimeimall.common.annotation.DubboAndHTTPExceptionCatch;
import com.kamistoat.meimeimall.common.dubboInterface.ProductDubboInterface;
import com.kamistoat.meimeimall.common.exception.BizCodeEnum;
import com.kamistoat.meimeimall.common.exception.RRException;
import com.kamistoat.meimeimall.common.utils.R;
import com.kamistoat.meimeimall.meimeimallsearch.service.ESSearchService;
import com.kamistoat.meimeimall.meimeimallsearch.vo.SearchParamVo;
import com.kamistoat.meimeimall.meimeimallsearch.vo.SearchResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "首页操作")
@RestController
@RequestMapping("web")
public class HomePageController {
    @Autowired
    ESSearchService esSearchService;

    @Reference
    ProductDubboInterface productDubboInterface;

    /**
     * 首页点击三级商品分类时，带缓搜索.
     * ES第一页的商品将位于缓存中，其他页的不入缓存。
     * @param categoryId 二级商品分类Id
     * @return
     */
    @ApiOperation("点击三级商品分类时，带缓搜索该分类下的ES商品第一页")
    @GetMapping("/home_page_category_search/{categoryId}")
    @DubboAndHTTPExceptionCatch
    public R categorySearch(@PathVariable("categoryId") Long categoryId) {
        R categoryLevelR = productDubboInterface.getCategoryLevel(categoryId);
        if (categoryLevelR.getCode() == 0) {
            int level = (int) categoryLevelR.get("categoryLevel");
            if (level == 3) {
                SearchResponseVo searchResponseVo = esSearchService.searchByCategoryFirstPage(categoryId);
                return R.ok().put("searchResponseVo", searchResponseVo);
            } else {
                throw new RRException(BizCodeEnum.CATEGORY_ID_BANNED.getMessage(), BizCodeEnum.CATEGORY_ID_BANNED.getCode());
            }
        } else {
            throw new RRException(categoryLevelR.getMsg(), categoryLevelR.getCode());
        }
    }


    /**
     * 首页直接在主搜索框以关键字搜索。不使用缓存。
     * @param keyword 关键字
     * @return
     */
    @ApiOperation("首页直接在主搜索框输入关键字搜索，不使用缓存")
    @GetMapping("/home_page_keyword_search/{keyword}")
    @DubboAndHTTPExceptionCatch
    public R searchBoxSearch(@PathVariable("keyword") String keyword) {
        SearchResponseVo searchResponseVo = esSearchService.searchByKeyword(keyword);
        return R.ok().put("searchResponseVo", searchResponseVo);
    }


    /**
     * 在商城首页点击三级菜单时或搜索按钮时，跳转到搜索页
     * 在搜索页继续搜索，还是该方法响应
     *
     * @param searchParamVo 查询条件Vo
     * @return 页面html文件名
     */
    @ApiOperation("商品检索")
    @GetMapping("/product_search")
    @DubboAndHTTPExceptionCatch
    public R listSearchPage(SearchParamVo searchParamVo, HttpServletRequest request) {
        // 用HttpServletRequest.getQueryString()可以把URL中从?开始之后的字符串提取出来
        String queryString = request.getQueryString();
        searchParamVo.setQueryString(queryString);
        // 调用服务层方法，根据查询条件Vo到ES中查询数据，并返回封装好的SearchResponseVo类
        SearchResponseVo listSearchResult = esSearchService.search(searchParamVo);
        return R.ok().put("listSearchResult", listSearchResult);
    }
}
